import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QSizePolicy
from PyQt5.QtSvg import QSvgWidget

import svgwrite
import math

def make_arrow(dwg, start_x, start_y, end_x, end_y, color="black", head_size=10, stroke_width=2):
    """
    Creates an arrow with a triangle head
    """
    # Calculate the angle of the line
    angle = math.atan2(end_y - start_y, end_x - start_x)
    
    # Calculate the position of the arrowhead base
    arrow_base_x = end_x - head_size * math.cos(angle)
    arrow_base_y = end_y - head_size * math.sin(angle)
    
    # Calculate the points for the arrowhead
    left_x = arrow_base_x - head_size/2 * math.cos(angle - math.pi/2)
    left_y = arrow_base_y - head_size/2 * math.sin(angle - math.pi/2)
    right_x = arrow_base_x - head_size/2 * math.cos(angle + math.pi/2)
    right_y = arrow_base_y - head_size/2 * math.sin(angle + math.pi/2)
    
    # Draw the line
    dwg.add(dwg.line(
        start=(start_x, start_y),
        end=(arrow_base_x, arrow_base_y),
        stroke=color,
        stroke_width=stroke_width
    ))
    
    # Draw the arrowhead
    dwg.add(dwg.polygon(
        points=[(end_x, end_y), (left_x, left_y), (right_x, right_y)],
        fill=color
    ))

def calculate_segment_center(center, radius, angle, offset_angle=0):
    """
    Calculate the center point of a segment at given angle and radius
    offset_angle: additional angle offset in degrees for overlapping pointers
    """
    adjusted_angle = angle + offset_angle
    x = center + radius * math.cos(math.radians(adjusted_angle))
    y = center + radius * math.sin(math.radians(adjusted_angle))
    return x, y

def make_svg(outer_diameter, inner_diameter, colors, num_segments, stroke_color, stroke_width, 
             head_index=0, tail_index=0, head_color="red", tail_color="blue", filename="output.svg"):
    # Adjust canvas size to accommodate stroke width and arrows
    padding = 40  # Reduced padding since we don't need space for labels
    canvas_size = outer_diameter + 2 * stroke_width + 2 * padding
    dwg = svgwrite.Drawing(filename, profile='tiny', size=(canvas_size, canvas_size))
    
    center = canvas_size / 2
    radius_outer = outer_diameter / 2
    radius_inner = inner_diameter / 2
    angle_step = 360 / num_segments

    # Draw the segments
    for i in range(num_segments):
        start_angle = i * angle_step
        end_angle = start_angle + angle_step
        color = colors[i % len(colors)]

        # Calculate the points for the outer arc
        x1_outer = center + radius_outer * math.cos(math.radians(start_angle))
        y1_outer = center + radius_outer * math.sin(math.radians(start_angle))
        x2_outer = center + radius_outer * math.cos(math.radians(end_angle))
        y2_outer = center + radius_outer * math.sin(math.radians(end_angle))

        # Calculate the points for the inner arc
        x1_inner = center + radius_inner * math.cos(math.radians(start_angle))
        y1_inner = center + radius_inner * math.sin(math.radians(start_angle))
        x2_inner = center + radius_inner * math.cos(math.radians(end_angle))
        y2_inner = center + radius_inner * math.sin(math.radians(end_angle))

        path_data = [
            f"M {x1_outer} {y1_outer}",
            f"A {radius_outer} {radius_outer} 0 0 1 {x2_outer} {y2_outer}",
            f"L {x2_inner} {y2_inner}",
            f"A {radius_inner} {radius_inner} 0 0 0 {x1_inner} {y1_inner}",
            "Z"
        ]

        dwg.add(dwg.path(d=" ".join(path_data), fill=color, stroke=stroke_color, stroke_width=stroke_width))

    # Calculate base angles for both pointers
    head_angle = head_index * angle_step + angle_step / 2
    tail_angle = tail_index * angle_step + angle_step / 2
    
    # Determine if pointers overlap and calculate offsets
    pointer_offset = 10  # degrees of offset when overlapping
    head_offset = pointer_offset if head_index == tail_index else 0
    tail_offset = -pointer_offset if head_index == tail_index else 0

    # Add head pointer with offset
    mid_radius = (radius_outer + radius_inner) / 2
    head_target_x, head_target_y = calculate_segment_center(center, mid_radius, head_angle, head_offset)
    head_start_x, head_start_y = calculate_segment_center(center, radius_outer + 30, head_angle, head_offset)
    make_arrow(dwg, head_start_x, head_start_y, head_target_x, head_target_y, 
              color=head_color, stroke_width=stroke_width)
    
    # Add tail pointer with offset
    tail_target_x, tail_target_y = calculate_segment_center(center, mid_radius, tail_angle, tail_offset)
    tail_start_x, tail_start_y = calculate_segment_center(center, radius_outer + 30, tail_angle, tail_offset)
    make_arrow(dwg, tail_start_x, tail_start_y, tail_target_x, tail_target_y, 
              color=tail_color, stroke_width=stroke_width)

    dwg.save()

# Example usage



# 示例调用
# make_svg(100, 40, ["#0000dd", "#00aaff"], 8, stroke_color="#000000", stroke_width=2)

class SvgViewer(QMainWindow):
    def __init__(self, svg_file):
        super().__init__()

        # Set up the main window
        self.setWindowTitle("SVG Viewer")
        self.setGeometry(100, 100, 400, 400)

        # Create a widget to hold the layout
        central_widget = QWidget(self)
        self.setCentralWidget(central_widget)

        # Create a vertical layout
        layout = QVBoxLayout(central_widget)

        # Create an SVG widget
        svg_widget = QSvgWidget(svg_file)
        layout.addWidget(svg_widget)
        # Enable scaling of the SVG content
        svg_widget.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
        # svg_widget.setScaledContents(True)  # This attribute does not exist for QSvgWidget

if __name__ == "__main__":
    app = QApplication(sys.argv)
    colors = ['#f0f0f0'] * 8  # Light gray for all segments
    make_svg(
        outer_diameter=200,
        inner_diameter=120,
        colors=colors,
        num_segments=8,
        stroke_color="black",
        stroke_width=2,
        head_index=0,
        tail_index=0,
        head_color="#FF4136",
        tail_color="#0074D9"
    )
    
    # svg_file = r"C:\Users\ZhouXiaokang\Downloads\c.svg"  # Replace with your SVG file path
    svg_file = "output.svg"
    viewer = SvgViewer(svg_file)
    viewer.show()
    sys.exit(app.exec_())
